const webpack = require('webpack');
const path = require('path');
const CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

let config = {
    entry: {
        server: ['webpack/hot/dev-server', 'webpack/hot/only-dev-server'],
        vendor_react: ['react', 'react-dom', 'react-router'],
        vendor_ant: ['antd'],
        index: path.resolve(__dirname, 'app/src/components/index/index.jsx')
    },
    resolve: {
        root: path.resolve('src'),
        extensions: ['', '.js', '.jsx'],
        modulesDirctories: ['node_modules']
    },
    devtool: 'eval',
    devServer: {
        contentBase: '/build/',
        devtool: 'eval',
        hot: true,
        inline: true,
        host: 'localhost',
        port: 8080,
        proxy: {
            '/api': {
                target: 'http://www.kuaidiapi.cn/',
                pathRewrite: {
                    '^/api': ''
                },
                changeOrigin: true,
                secure: false
            }
        }
    },
    output: {
        publicPath: '/build/',
        path: '/build',
        filename: '[name].bundle.js'
    },
    module: {
        preLoaders: [
            {
                test: /\.jsx?$/,
                loader: 'eslint-loader',
                include: [path.resolve(__dirname, 'src')],
                exclude: /node_modules/
            }
        ],
        loaders: [
            {
                test: /\.jsx?$/,
                exclude: /node_modules/,
                loaders: ['babel?presets[]=es2015,presets[]=react']
            },
            {
                test: /\.s?css$/,
                loader: ExtractTextPlugin.extract('style-loader', 'css-loader?importLoaders=1', 'postcss-loader?parser=postcss-scss')
            },
            {
                test: /\.html$/,
                loaders: [
                    'html-loader?' + JSON.stringify({
                        attrs: ["img:src", "link:href"]
                    }),
                    // 'file-loader?name=[path][name].[ext]',
                    // 'extract-loader'
                ]
            }
        ]
    },
    cache: false,
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),
        new CommonsChunkPlugin({
            name: 'vendor_hotserver',
            filename: 'vendor_hotserver.js',
            chunks: ['vendor_hotserver']
        }),
        new CommonsChunkPlugin({
            name: 'vendor_react',
            filename: 'vendor_react.js',
            chunks: ['vendor_react']
        }),
        new CommonsChunkPlugin({
            name: 'vendor_antd',
            filename: 'vendor_antd.js',
            chunks: ['vendor_antd']
        }),
        new HtmlWebpackPlugin({
            title: 'My App',
            filename: 'index.html',
            template: 'app/www/index/index.html',
            inject: 'body',
            chunks: ['index', 'vendor_react', 'vendor_antd', 'vendor_hotserver']
        }),
        new ExtractTextPlugin("[name].css")
    ]
}

module.exports = config;